<?php

namespace Tbf\Bundle\YntAuthBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Yaml\Yaml;

/**
 * This is the class that loads and manages your bundle configuration
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
 */
class TbfYntAuthExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');

        $container->setParameter('tbf_ynt_auth.denglu.app_key',$config['denglu']['app_key']);
        $container->setParameter('tbf_ynt_auth.denglu.app_secret',$config['denglu']['app_secret']);
        $container->setParameter('tbf_ynt_auth.douban.app_key',$config['douban']['app_key']);
        $container->setParameter('tbf_ynt_auth.douban.app_secret',$config['douban']['app_secret']);
        $this->loadMediaManager($container);
    }
    public function loadMediaManager(ContainerBuilder $container){
        $media_array = $this->getVendorMap();
        $media_define_array = array();
        foreach($media_array as $row){
            $def = new Definition('Tbf\Bundle\YntAuthBundle\Model\Media');
            $media_define_array[$row['name_en']] = $def->setArguments(array(
                $row['name_en'],
                $row['name_ch'],
                $row['name_short_ch'],
            ));
        }
        foreach($media_define_array as $media_define){
            $container->getDefinition('tbf_ynt_auth.media_manager')->addMethodCall(
                'registerMedia',array($media_define)
            );
        }
        $lazy_name_en_imp_service_id_map = array();
        foreach($media_array as $row){
            $lazy_name_en_imp_service_id_map[$row['name_en']] = $row['imp_id'];
        }
        $container->getDefinition('tbf_ynt_auth.media_manager')
            ->addArgument(new Reference('service_container'))
            ->addArgument($lazy_name_en_imp_service_id_map)
            ->addArgument(array('qq'=>'qzone'))
            ;

        $denglu_media_id_name_en_map = array();
        foreach($media_array as $row){
            $denglu_media_id_name_en_map[$row['denglu_id']] = $row['name_en'];
        }
        $container->getDefinition('tbf_ynt_auth.denglu.media_id_map')
            ->addArgument($denglu_media_id_name_en_map)
            ->addArgument(new Reference('tbf_ynt_auth.media_manager'))
        ;
    }
    protected  function getVendorMap(){
        $data =  Yaml::parse(<<<YML
- { name_en: douban, name_ch: 豆瓣, denglu_id: 9, imp_id: tbf_ynt_auth.douban}
- { name_en: qzone, name_ch: QQ, denglu_id: 13, imp_id: tbf_ynt_auth.denglu }
- { name_en: renren, name_ch: 人人, denglu_id: 7, imp_id: tbf_ynt_auth.denglu }
- { name_en: kaixin, name_ch: 开心, denglu_id: 8, imp_id: tbf_ynt_auth.denglu }
- { name_en: sina, name_ch: 新浪微博, denglu_id: 3, imp_id: tbf_ynt_auth.denglu ,name_short_ch: 新浪}
- { name_en: tencent, name_ch: 腾讯微博, denglu_id: 4, imp_id: tbf_ynt_auth.denglu ,name_short_ch: 腾讯}
- { name_en: sohu, name_ch: 搜狐微博, denglu_id: 5, imp_id: tbf_ynt_auth.denglu ,name_short_ch: 搜狐}
- { name_en: netease, name_ch: 网易微博, denglu_id: 6, imp_id: tbf_ynt_auth.denglu ,name_short_ch: 网易}
- { name_en: alipayquick, name_ch: 支付宝快捷, denglu_id: 18, imp_id: tbf_ynt_auth.denglu ,name_short_ch: 支付宝}
- { name_en: taobao, name_ch: 淘宝, denglu_id: 16, imp_id: tbf_ynt_auth.denglu }
- { name_en: google, name_ch: 谷歌, denglu_id: 1, imp_id: tbf_ynt_auth.denglu }
- { name_en: windowslive, name_ch: MSN, denglu_id: 2, imp_id: tbf_ynt_auth.denglu }
- { name_en: yahoo, name_ch: 雅虎, denglu_id: 12, imp_id: tbf_ynt_auth.denglu }
- { name_en: baidu, name_ch: 百度, denglu_id: 19, imp_id: tbf_ynt_auth.denglu }
- { name_en: tianya, name_ch: 天涯, denglu_id: 17, imp_id: tbf_ynt_auth.denglu }
- { name_en: netease163, name_ch: 网易通行证, denglu_id: 21, imp_id: tbf_ynt_auth.denglu ,name_short_ch: 163}
- { name_en: guard360, name_ch: '360', denglu_id: 23, imp_id: tbf_ynt_auth.denglu }
- { name_en: tianyi, name_ch: 天翼189, denglu_id: 26, imp_id: tbf_ynt_auth.denglu ,name_short_ch: 天翼}
- { name_en: facebook, name_ch: Facebook, denglu_id: 27, imp_id: tbf_ynt_auth.denglu ,name_short_ch: fb}
- { name_en: twitter, name_ch: Twitter, denglu_id: 28, imp_id: tbf_ynt_auth.denglu}
YML
        );
        foreach($data as $key=>$row){
            if (empty($row['name_short_ch'])){
                $data[$key]['name_short_ch'] = null;
            }
        }
        return $data;
    }
}
